
class Solution {
    public int longestOnes(int[] nums, int k) {
        int left = 0, right = 0, n = nums.length;
        int ret = 0, zero = 0;
        while(right < n) {
            // 1、进窗口
            while(right < n && (nums[right] != 0 || zero < k)) {
                if(nums[right] == 0) zero++;
                right++;
            }
            // 2、更新结果
            ret = Math.max(ret, right - left);
            // 3、出窗口
            while(nums[left] != 0) {
                left++;
            }
            left++;
            if(nums[left] == 0 && zero > 0) zero -= 1;
        }
        return ret;
    }
}